GLTexture [] scaleTex = new GLTexture[numScaleTextures];
GLTexture scaleGlowTex;

class ScaleGrid
{
  Scale [][] scales;
  float scaleOverlappedWidth;
  float scaleOverlappedHeight;
  
  ScaleGrid(float windowWidth, float windowHeight, float scaleSize, float scaleOverlapPercentX, float scaleOverlapPercentY)
  { 
    scaleOverlappedWidth  = scaleSize*(1.0-scaleOverlapPercentX);
    scaleOverlappedHeight = scaleSize*(1.0-scaleOverlapPercentY);
    
    // figure out how many scales we will need to fill the screen
    float oddRowStartX  = -scaleSize / 2.0;
    float evenRowStartX = oddRowStartX - scaleOverlappedWidth / 2.0;
  //  float startY        = -scaleOverlappedHeight;
    float startY        = -scaleSize + scaleOverlappedHeight;
    
    int numScalesWide  = ceil((width + abs(evenRowStartX) * 2.0) / scaleOverlappedWidth);
    int numScalesTall  = ceil((height + abs(startY)) / scaleOverlappedHeight);
    
    println("overlapped scale width: " + scaleOverlappedWidth);
    println("overlapped scale height: " + scaleOverlappedHeight);
    println("num wide: " + numScalesWide);
    println("num tall: " + numScalesTall);
    
    // create our 'grid' of scales
    scales = new Scale[numScalesTall][];
    for( int i = 0; i < numScalesTall; i++ )
    {
      scales[i]  = new Scale[numScalesWide - (i%2)];
      float offsetX  = evenRowStartX;
      if( i%2 != 0 )
        offsetX  = oddRowStartX;
      float offsetY  = startY;
      
      for( int j = 0; j < scales[i].length; j++ )
      {
        scales[i][j]  = new Scale(offsetX + float(j) * scaleOverlappedWidth,
                                  offsetY + float(i) * scaleOverlappedHeight,
                                  scaleSize, scaleSize,
                                  scaleTex[floor(random(13))]); 
      }
    }
    
    // set neighbour pointers of scales
    for( int row = 0; row < scales.length; row++ )
    {
      for( int col = 0; col < scales[row].length; col++ )
      {
        Scale pL = null, pR = null, nL = null, nR = null, cL = null, cR = null;
        
        if(row%2 == 0)
        {
          // even row, contains numScalesWide scales
          if(row > 0)
          {
            // parents
            if(col > 0)
              pL  = scales[row-1][col-1];
            if(col < numScalesWide-1)
              pR  = scales[row-1][col];
          }
          {
            // neighbours
            if(col > 0)
              nL  = scales[row][col-1];
            if(col < numScalesWide-1)
              nR  = scales[row][col+1];
          }
          if(row < numScalesTall-1)
          {
            // children
            if(col > 0)
              cL  = scales[row+1][col-1];
            if(col < numScalesWide-1)
              cR  = scales[row+1][col];
          }
        }
        else
        {
          // odd row, contains numScalesWide - 1 scales
          if(row > 0)
          {
            // parents
            pL  = scales[row-1][col];
            pR  = scales[row-1][col+1];
          }
          {
            // neighbours
            if(col > 0)
              nL  = scales[row][col-1];
            if(col < numScalesWide-2)
              nR  = scales[row][col+1];
          }
          if(row < numScalesTall-1)
          {
            // children
            cL  = scales[row+1][col];
            cR  = scales[row+1][col+1];
          }
        }
          
        scales[row][col].setNeighbours(pL,pR,nL,nR,cL,cR); 
      }
    }
  }
  
  void update(float milliseconds, Vector points)
  {
    boolean scaleTouched = false;
    
    // update each fishscale
    for( int row = 0; row < scales.length; row++ )
    {
      for( int col = 0; col < scales[row].length; col++ )
      {
        for( int i = 0; i < points.size(); i++ )
        {
          PVector vec = (PVector)points.elementAt(i);
          if(scales[row][col].collides(vec.x, vec.y))
          {
            scales[row][col].touch(milliseconds);
          }
        }
        
        scales[row][col].update(milliseconds);
      }
    }
  }  
  
  void render(GLGraphicsOffScreen _gl)
  {
    // render our 'grid' of fishscales, starting at the bottom row
    for( int row = scales.length-1; row >= 0; row-- )
    {
      for( int col = 0; col < scales[row].length; col++ )
      {
        scales[row][col].render(_gl);   
      }
    }
  }
  
  
}


